package com.tcc.task.personal;

import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.tcc.login.logic.Login;

public class CreateTask {
	WebDriver driver = new FirefoxDriver();
	String categoryName = "Work";
	String []month={"January","February","March","April","May","June","July","August","September","October","November","December"};
	String notes = "Selenium automates browsers. That's it. What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.";

	@BeforeTest(groups={"Test1"})
	public void setUp() throws InterruptedException {
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		Login myLogin = new Login();
		myLogin.testLogin(driver);
	}
	@Test(description="Test whether the alert will appear when title is null.", priority=1)
	public void titleIsNull(){
		driver.get("https://prod.campuscruiser.com/q?cx=u&pg=papp&tg=Tasks-editTask");
		driver.findElement(By.cssSelector("img.cc16")).click();
		driver.switchTo().frame(driver.findElement(By.id("datePicker")));
		driver.findElement(
				By.xpath("//table[@id='ccCalTable']/tbody/tr[2]/td[3]/a"))
				.click();
		driver.switchTo().defaultContent();
       
		driver.findElement(By.id("ccEditorrtfDetail")).sendKeys(notes);

		driver.findElement(By.xpath("//input[@value='Save']")).click();
			Assert.assertTrue(driver.switchTo().alert().getText().contains("Title"));
			driver.switchTo().alert().accept();
	}
	@Test(description="Create and check Task1", priority=2, groups={"Test1"})
	public void Task1(){
		driver.get("https://prod.campuscruiser.com/q?cx=u&pg=papp&tg=Tasks-editTask");
		driver.findElement(By.name("title")).sendKeys("Task1");
		driver.findElement(
				By.xpath("//select[@id='statusId']/option[@value='0']")).click();
		driver.findElement(By.cssSelector("img.cc16")).click();
		driver.switchTo().frame(driver.findElement(By.id("datePicker")));
		driver.findElement(By.xpath("//body/form/table/tbody/tr[2]/td/div/table/tbody/tr/td[2]/a/img")).click();	
		driver.findElement(By.xpath("//table[@id='ccCalTable']/tbody/tr[2]/td[2]/a")).click();
		driver.switchTo().defaultContent();   
		String dueDate=driver.findElement(By.id("dueDate")).getAttribute("value");
		driver.findElement(By.id("linkToCal")).click();
		driver.findElement(By.id("ccEditorrtfDetail")).sendKeys(notes); 
		// Used to upload picture.
		driver.findElement(By.xpath("//*[@id='ccEditorrtfDetailImage']/img")).click();
		driver.findElement(By.id("ccEditorUploadMsgImage")).click();
		for(String handle : driver.getWindowHandles()){
			 driver.switchTo().window(handle);
		}
		driver.findElement(By.id("imageText")).sendKeys("This is a png picture");
		driver.findElement(By.xpath("html/body/form[2]/table/tbody/tr/td[2]/input[1]")).click();
		Assert.assertEquals(driver.switchTo().alert().getText(), "Please select an image to upload!");
		driver.switchTo().alert().accept();
		File picFile= new File("C:/Users/Administrator.zxcv-PC/Desktop/QQ20120518134719.png");
		driver.findElement(By.id("fileName")).sendKeys(picFile.getAbsolutePath());
		driver.findElement(By.xpath("html/body/form[2]/table/tbody/tr/td[2]/input[1]")).click(); //upload button
		driver.findElement(By.xpath("html/body/form/div[2]/input[1]")).click();//apply button
		//driver.switchTo().defaultContent();
		String windowId="";
		for(String handle : driver.getWindowHandles()){
			if(driver.switchTo().window(handle).getTitle().contains("Task Editor")){
				windowId=handle;
				break;
			}
			driver.switchTo().window(windowId);
		}
		driver.findElement(By.xpath("//input[@value='Save']")).click();
		@SuppressWarnings("deprecation")
		Date newDate=new Date(dueDate);
		Locale locale=new Locale("en","US");
		DateFormat dateFormat=new SimpleDateFormat("EE, MMM dd, yyyy", locale);
		String dateString=dateFormat.format(newDate);
		Assert.assertTrue(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]")).getText().endsWith("Task1"));
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).getAttribute("class"),"ccAlert");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[3]")).getText(), "1");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[4]")).getText(), dateString);
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[4]/span")).getAttribute("class"),"ccAlert");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[5]")).getText(), "New");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[6]")).getText(), "");
		driver.get("https://prod.campuscruiser.com/q?pg=papp&a=calendar&cx=u#");
		driver.findElement(By.id("prevBtn")).click();
		Assert.assertEquals(driver.findElement(By.xpath("//tbody[@id='mTbody']/tr[2]/td[2]/div[2]")).getAttribute("class"),"eventList");
		
	}
	@Test(description="Create and check Task2", priority=3)
	public void Task2(){
		driver.get("https://prod.campuscruiser.com/q?cx=u&pg=papp&tg=Tasks-editTask");
		driver.findElement(By.name("title")).sendKeys("Task2");
		new Select(driver.findElement(By.id("category"))).selectByVisibleText(categoryName);
        driver.findElement(
		By.xpath("//select[@id='priority']/option[3]")).click();
        driver.findElement(
		By.xpath("//select[@id='statusId']/option[2]")).click();
		driver.findElement(By.cssSelector("img.cc16")).click();
		driver.switchTo().frame(driver.findElement(By.id("datePicker")));
		driver.findElement(
				By.xpath("html/body/form/table/tbody/tr[2]/td/div/table/tbody/tr/td[3]/a")).click();
		driver.switchTo().defaultContent();        
		driver.findElement(By.xpath("//*[@id='ccEditorrtfDetailImage']/img")).click();
		driver.findElement(By.id("ccEditorUploadMsgImage")).click();
		for(String handle : driver.getWindowHandles()){
			 driver.switchTo().window(handle);
		}
		File picFile= new File("C:/Users/Administrator.zxcv-PC/Desktop/QQ20120518134719.png");
		driver.findElement(By.id("fileName")).sendKeys(picFile.getAbsolutePath());
		driver.findElement(By.id("imageText")).sendKeys("This is a png picture");
		driver.findElement(By.xpath("html/body/form[2]/table/tbody/tr/td[2]/input[1]")).submit();
		
		driver.findElement(By.xpath("html/body/form/div[2]/input[1]")).click();
		for(String handle : driver.getWindowHandles()){
			 driver.switchTo().window(handle);
		}
	//	driver.switchTo().defaultContent();
		//driver.findElement(By.id("ccEditorrtfDetail")).sendKeys(picFile.getAbsolutePath()); 
		String dueDate=driver.findElement(By.id("dueDate")).getAttribute("value");
		driver.findElement(By.xpath("//input[@value='Save']")).click();
		@SuppressWarnings("deprecation")
		Date newDate=new Date(dueDate);
		Locale locale=new Locale("en","US");
		DateFormat dateFormat=new SimpleDateFormat("EE, MMM dd, yyyy", locale);
		String dateString=dateFormat.format(newDate);
		Assert.assertTrue(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]")).getText().endsWith("Task2"));
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).getAttribute("class"),"");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[3]")).getText(), "3");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[4]")).getText(), dateString);
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[5]")).getText(), "1");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[6]")).getText(), categoryName);
	}
	@Test(description="Create and check Task3", priority=4)
	public void Task3(){
		driver.get("https://prod.campuscruiser.com/q?cx=u&pg=papp&tg=Tasks-editTask");
		driver.findElement(By.name("title")).sendKeys("Task3");
		new Select(driver.findElement(By.id("category"))).selectByVisibleText(categoryName);
        driver.findElement(
		By.xpath("//select[@id='priority']/option[5]")).click();
        driver.findElement(By.xpath("//select[@id='statusId']/option[@value='1']")).click();
		driver.findElement(By.cssSelector("img.cc16")).click();
		driver.switchTo().frame(driver.findElement(By.id("datePicker")));
		new Select(driver.findElement(By.xpath("html/body/form/table/tbody/tr[1]/td[1]/select"))).selectByIndex(6);
		driver.findElement(
				By.xpath("//table[@id='ccCalTable']/tbody/tr[2]/td[3]/a")).click();
		driver.switchTo().defaultContent();        
		driver.findElement(By.id("ccEditorrtfDetail")).sendKeys(notes); 
		String dueDate=driver.findElement(By.id("dueDate")).getAttribute("value");
		driver.findElement(By.xpath("//input[@value='Save']")).click();
		@SuppressWarnings("deprecation")
		Date newDate=new Date(dueDate);
		Locale locale=new Locale("en","US");
		DateFormat dateFormat=new SimpleDateFormat("EE, MMM dd, yyyy", locale);
		String dateString=dateFormat.format(newDate);
	/*	Assert.assertTrue(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]")).getText().endsWith("Task2"));
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).getAttribute("class"),"");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[3]")).getText(), "3");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[4]")).getText(), dateString);
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[5]")).getText(), "1");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[6]")).getText(), categoryName);
		*/
		Assert.assertTrue(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[2]")).getText().endsWith("Task3"));
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[2]/a/span")).getAttribute("class"),"ccStrike");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[3]")).getText(), "5");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[4]")).getText(), dateString);
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[4]/span")).getAttribute("class"), "ccStrike");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[5]")).getText(), "Completed");
		Assert.assertEquals(driver.findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[6]")).getText(), categoryName);
	}
	@Test(description="Check and delete all the tasks", priority=5)
	public void deleteTask(){
		driver.get("https://prod.campuscruiser.com/q?pg=papp&a=tasks&v=listTasks&cx=u");
		driver.findElement(By.xpath("//table[@id='taskTable']/thead/tr[1]/th[1]")).click();
		driver.findElement(By.xpath("//*[@id='ccTgArea']/form/table[2]/tbody/tr/td[2]/table/tbody/tr/td[4]")).click();
		Assert.assertEquals(driver.switchTo().alert().getText(), "Do you really want to delete the selected item(s)?");
		driver.switchTo().alert().accept();
	}
}
